package springapp.service;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import springapp.dao.SalaryDao;
import springapp.entity.SalaryEntity;

@Service
public class SalaryService {

	@Autowired(required = true)
	private SalaryDao salaryDao;

	@Autowired(required = true)
	private IDesignationService designationService;

	@Autowired(required = true)
	private ILocationService locationService;

	@Transactional
	public SalaryEntity addSalary(String gender, Float salary,
			Integer locationId, Integer designationId, Integer experience,
			Integer companyId, String jobType) {
		SalaryEntity salaryEntity = new SalaryEntity(gender, salary,
				locationId, designationId, experience, jobType);
		this.salaryDao.saveEntity(salaryEntity);
		return salaryEntity;
	}

	@Transactional
	public SalaryEntity getSalaryEntity(int id) {
		return this.salaryDao.getEntity(id);
	}

	public void populateSalaries(Collection<SalaryEntity> salaries) {
		for (SalaryEntity salaryEntity : salaries) {
			salaryEntity.setDesignationName(designationService
					.getDesignationName(salaryEntity.getDesignationId()));
			salaryEntity.setLocationName(locationService
					.getLocationName(salaryEntity.getLocationId()));
		}
	}

	@Transactional
	public SalaryEntity[] getSalaryForCompanyId(Integer companyId) {
		List<SalaryEntity> salaries = this.salaryDao
				.getSalariesForCompanyId(companyId);
		populateSalaries(salaries);
		return salaries.toArray(new SalaryEntity[0]);
	}

	@Transactional
	public int getNumSalaries(Integer companyId) {
		return this.salaryDao.getNumSalariesForCompany(companyId);
	}

}
